window.onload=function(){
    
     //日期更新
    var mock = Mock.mock({
        nowDate: "@now('yyyy年MM月dd日')"
    })
    document.getElementsByClassName("date")[0].innerHTML = `<p>${mock.nowDate}</p>
                                                            <p>${" 星期" + "日一二三四五六".charAt(new Date().getDay())}</p>`;
    
    
    
    // 调取三大指数信息
    function indexUpDown() {
        ajax({
            type: 'get',
            dataType: 'json',
            data: {
                secu_codes: "sh000001,sz399001,sz399006",
                fields: "secu_code,secu_name,change,last_px,preclose_px"
            },
            url: 'http://127.0.0.1:8000/basic',
            success: function (res) {
                var upDownData = document.getElementsByClassName("upDownData")[0];
                upDownData.innerHTML = "";
                UpDown.showUpDownData(res.data, ".upDownData");
            }
        })
    }



    // var setTimeUpdate = setInterval(function () {
        indexUpDown();
        progress();
        TodayData("gnbk");
        TodayData("hybk");
        TodayData("dybk");
        topNo("desc", "percent");
    // }, 3000);





    // 进度条
    function progress() {
        ajax({
            type: 'get',
            dataType: 'json',
            url: 'http://127.0.0.1:8000/home',
            success: function (res) {
                document.getElementsByClassName("down")[0].innerText = res.data.up_down_dis.fall_num + "家";
                document.getElementsByClassName("up")[0].innerText = res.data.up_down_dis.rise_num + "家";
                //进度条
                var progress = document.getElementsByClassName("progress")[0];
                liList = progress.getElementsByTagName("li");
                liList[0].style.width = 0.39 + res.data.up_down_dis.fall_num * 0.0009 + "rem";
                liList[2].style.width = 3.724 - res.data.up_down_dis.fall_num * 0.0009 + "rem";
            }
        })
    }


    //同花顺头条
    var swiper_wrapper = document.getElementsByClassName('swiper-wrapper')[0];
    var titleDate = document.getElementsByClassName("titleDate")[0];
    var chevron_down = document.getElementById("chevron_down");
    var flag = true;
    chevron_down.onclick = function () {
        if (flag) {
            swiper_wrapper.style.opacity = "0";
            this.className = "fa fa-chevron-down active";
            titleDate.style.display = "block";
            flag = false;
        } else {
            swiper_wrapper.style.opacity = "1";
            this.className = "fa fa-chevron-down";
            titleDate.style.display = "none";
            flag = true;
        }
    }




    var nowDate = parseInt(new Date().getTime() / 1000);
    //轮播标题数据请求
    (function () {
        ajax({
            type: 'get',
            dataType: 'json',
            data: {
                lastTime: nowDate,
                last_time: nowDate,
                refresh_type: 1,
                rn: 20
            },
            url: 'http://127.0.0.1:8000/telegraphList',
            success: function (res) {
                Title.showTitleData(res.data.roll_data, ".titleDate");
                renderTitle(res.data.roll_data);
            }
        })
    })();




    //轮播
    function renderTitle(data) {
        var num = 0;
        var swiper_slide = document.getElementsByClassName("swiper-slide");
        for (var i = 0; i < swiper_slide.length; i++) {
            if (i == 2) {
                num = -1;
            }
            num++;
            swiper_slide[i].innerText = data[num].content;
        }
    }




    var swiper = new Swiper('.swiper-container', {
        speed: 500,
        autoplay: true,
        direction: 'vertical', // 垂直切换选项
        loop: true// 循环模式选项
    });
    //数据刷新
    var setTime = setInterval(period, 10000);
    // 9：15至9：25为集合竞价时间
    //开盘中9:30到11:30
    //午间休盘11:30到13:00
    //开盘中13：00到15:00
    //其余时间均为已收盘  






    var picture = document.getElementsByClassName("picture");
    //时期更新
    function period() {
        var systemTime = new Date();
        var hours = systemTime.getHours();
        var min = systemTime.getMinutes();
        var ss = systemTime.getSeconds();
        var dis = hours * 3600 + min * 60 + ss;
        //集合竞价
        var biddingStartTime = 9 * 3600 + 15 * 60;
        var biddingEndTime = 9 * 3600 + 25 * 60;
        //开盘中
        var openStartTime1 = 9 * 3600 + 30 * 60;
        var openEndTime1 = 11 * 3600 + 30 * 60;
        var openStartTime2 = 13 * 3600;
        var openEndTime2 = 15 * 3600;
        //午间休盘
        var middayStartTime = 11 * 3600 + 30 * 60;
        var middayEndTime = 13 * 3600;
        var text = document.getElementsByClassName("text")[0];
        if (dis > biddingStartTime && dis < biddingEndTime) {
            text.innerText = "集合竞价";
            for (var i = 0; i < picture.length; i++) {
                picture[i].style.display = "none";
            }
            picture[0].style.display = "block";
        } else if ((dis > openStartTime1 && dis < openEndTime1) || (dis > openStartTime2 && dis < openEndTime2)) {
            text.innerText = "开盘中";
            for (var i = 0; i < picture.length; i++) {
                picture[i].style.display = "none";
            }
            picture[0].style.display = "block";
        } else if (dis > middayStartTime && dis < middayEndTime) {
            text.innerText = "午间休盘";
            for (var i = 0; i < picture.length; i++) {
                picture[i].style.display = "none";
            }
            picture[3].style.display = "block";
        } else {
            text.innerText = "已收盘";
            for (var i = 0; i < picture.length; i++) {
                picture[i].style.display = "none";
            }
            picture[1].style.display = "block";
        }
    }
    period();





    // 热门概念：list: 'gnbk'
    // 热门行业：list: 'hybk'
    // 热门地域: list: 'dybk'
    function TodayData(list) {
        ajax({
            type: 'get',
            dataType: 'json',
            //当变量名和data内的变量名相同时，可以省略后面的
            data: {
                list
                //相当于list:list
            },
            url: 'http://127.0.0.1:8000/hot_plate',
            success: function (res) {
                if (this.data.list == "gnbk") {
                    var concept = document.getElementsByClassName("concept")[0];
                    concept.innerHTML = "";
                    Today.showTodayData(res, ".concept");
                } else if (this.data.list == "hybk") {
                    var concept = document.getElementsByClassName("industry")[0];
                    concept.innerHTML = "";
                    Today.showTodayData(res, ".industry");
                } else if (this.data.list == "dybk") {
                    var concept = document.getElementsByClassName("area")[0];
                    concept.innerHTML = "";
                    Today.showTodayData(res, ".area");
                }
            }
        })
    }




    //排行榜数据排
    var change2 = document.getElementsByClassName("change2")[0];
    var list1 = change2.getElementsByTagName("li");
    for (let i = 0; i < list1.length; i++) {
        list1[i].onclick = function () {
            for (var j = 0; j < list1.length; j++) {
                list1[j].className = "";
            }
            this.className = "active";
        }
    }






    var num = 0;
    var sort = document.getElementsByClassName("sort");
    var change2 = document.getElementsByClassName("change2")[0];
    var list2 = change2.getElementsByTagName("li");
    var top = document.getElementsByClassName('top')[0];
    for (let i = 0; i < list2.length; i++) {
        list2[i].onclick = function () {
            top.innerHTML = "";
            for (var j = 0; j < list2.length; j++) {
                list2[j].className = "";
            }
            this.className = "active";
            if (i === 0) {
                num = i;
                sort[0].style.display = "block";
                sort[1].style.display = "none";
                topNo("desc", "percent");
            } else if (i === 1) {
                num = i;
                sort[0].style.display = "none";
                sort[1].style.display = "block";
                topNo("asc", "percent");
            } else if (i === 2) {
                num = i;
                sort[0].style.display = "block";
                sort[1].style.display = "none";
                topNo('desc', "amount");
            
        }
        }
    }




    //涨跌幅
    function topNo(order, order_by) {
        ajax({
            type: 'get',
            dataType: 'json',
            data: {
                order: order,
                order_by: order_by
            },
            url: 'http://127.0.0.1:8000/index_stock_list',
            success: function (res) {
                var top = document.getElementsByClassName("top")[0];
                top.innerHTML = "";
                Top.topData(res.data.list, ".top");
            }
        })
    }




    //涨跌幅切换
    for (let i = 0; i < sort.length; i++) {
        sort[i].onclick = function () {
            var top = document.getElementsByClassName('top')[0];
            top.innerHTML = "";
            if (num === 0) {
                topNo("desc", "percent");
            } else if (num === 1) {
                topNo("asc", "percent");
            } else if (num === 2) {
                topNo('desc', "amount");
            }
            for (var j = 0; j < sort.length; j++) {
                sort[j].style.display = "block";
            }
            this.style.display = "none"
        }
    }






    //节假日数据请求
    // ajax({
    // type: 'get',
    // dataType: 'json',
    // data: {
    // $date: "2021-10-01"
    // },
    // url: 'http://timor.tech/api/holiday/info',
    // success: function (res) {
    // console.log(res);
    // }
    // })





    //三大指数
    function UpDown(upDownData) {
        this.li = document.createElement("li");
        var p1 = document.createElement("p");
        var p2 = document.createElement("p");
        var div = document.createElement("div");
        var p3 = document.createElement("p");
        var p4 = document.createElement("p");
        p1.innerText = upDownData.secu_name;
        p2.innerText = Math.round(upDownData.last_px * 100) / 100;
        if (upDownData.change * 100 < 0) {
            this.li.style.boxShadow = "0px 3px 25px 7px #dcfadd";
            this.li.style.background = "linear-gradient(to right, #62c30f, #2fad06, #089c01)";
        }
        var changeNum = upDownData.change * 100 < 0 ? (upDownData.change * 100).toFixed(2) + "%" : "+" + (upDownData.change * 100).toFixed(2) + "%";
        var reduceVal = (upDownData.last_px - upDownData.preclose_px) < 0 ? Math.round((upDownData.last_px - upDownData.preclose_px) * 100) / 100 : "+" + Math.round((upDownData.last_px - upDownData.preclose_px) * 100) / 100;
        p3.innerText = reduceVal;
        p4.innerText = changeNum;
        div.appendChild(p3);
        div.appendChild(p4);
        this.li.appendChild(p1);
        this.li.appendChild(p2);
        this.li.appendChild(div);
    }



    UpDown.showUpDownData = function (data, className) {
        var ul = document.querySelector(className);
        for (var obj in data) {
            ul.appendChild(new this(data[obj]).li);
        }
    }





    //今日模块[热门概念,热门行业,热门地域]
    function Today(data, index) {
        this.li = document.createElement("li");
        if (index % 2 == 0) {
            for (var i = 0; i < 4; i++) {
                var p = document.createElement("p");
                if (i == 0) {
                    p.innerText = data[2];
                } else if (i == 1) {
                    p.innerText = (Number(data[3]) * 10).toFixed(1) + "%";
                } else if (i == 2) {
                    p.innerText = data[4];
                } else if (i == 3) {
                    p.innerText = data[10] + "%";
                }
                this.li.appendChild(p);
            }
        }
    }





    Today.showTodayData = function (data, className) {
        var self = this;
        var ul = document.querySelector(className);
        data.forEach(function (item, index) {
            if (index < 5) {
                ul.appendChild(new self(item, index).li);
            }
        });
    }
    
    //轮播标题
    function Title(titleData, index) {
        this.p = document.createElement("p");
        var label1 = document.createElement("label");
        label1.innerText = index + 1;
        var label2 = document.createElement("label");
        label2.innerText = titleData.content;
        this.p.appendChild(label1);
        this.p.appendChild(label2);
    }


    Title.showTitleData=function(data, className){
        var div = document.querySelector(className);
        var self = this;
        data.forEach(function (item, index) {
            if (index < 3) {
                div.appendChild(new self(item, index).p);
            }
        });
    }



    //排行榜数据
    function Top(data) {
        this.li = document.createElement("li");
        var ul = document.createElement("ul");
        var li1 = document.createElement("li");
        var li2 = document.createElement("li");
        var li3 = document.createElement("li");
        var li4 = document.createElement("li");
        var p1 = document.createElement("p");
        p1.className = "stockInfo1";
        var p2 = document.createElement("p");
        p2.className = "stockInfo2";
        p1.innerText = data.name;
        p2.innerText = data.symbol.substr(2);
        li1.appendChild(p1);
        li1.appendChild(p2);
        ul.appendChild(li1);
        li2.innerText = data.current;
        if (data.percent < 0) {
            li3.style.color = "#009a00";
        }
        li3.innerText = data.percent > 0 ? "+" + data.percent + "%" : data.percent + "%";
        if (data.percent5m === 0) {
            data.percent5m = data.percent5m.toFixed(2) + "%";
        } else if (data.percent5m > 0) {
            data.percent5m = "+" + data.percent5m + "%";
            li4.style.color = "#d6423a";
        } else if (data.percent5m < 0) {
            data.percent5m = data.percent5m + "%";
            li4.style.color = "#009a00";
        }
        li4.innerText = data.percent5m;
        ul.appendChild(li2);
        ul.appendChild(li3);
        ul.appendChild(li4);
        this.li.appendChild(ul);

    }
    

    Top.topData=function(data, className){
        var self = this;
        var ul = document.querySelector(className);
        if (sort[1].style.display === "block") {
            data.sort(function (a, b) {
                return a.percent - b.percent;
            });
        } else {
            data.sort(function (a, b) {
                return b.percent - a.percent;
            });
        }
        data.forEach(function (item) {
            ul.appendChild(new self(item).li);
        });
    }

    var parentWindow = window.parent;
    var iframe = parentWindow.document.getElementsByTagName("iframe")[0];
    var mainsearch = document.querySelector(".mainsearch");
    //首页的股票指数页面点击搜索框事件
    mainsearch.addEventListener("click",function(){
        parentWindow.dealData.set("fromHome",true);
        iframe.src="../views/selfChoose.html";
    });




    top.addEventListener("click",function(event){
        var target=event.target||event.srcElement;
        if(target.nodeName==="LI"){
            parentWindow.dealData.set("stock_name", target.querySelector(".stockInfo1").innerText);
            parentWindow.dealData.set("stock_code", target.querySelector(".stockInfo2").innerText);
            iframe.src = "../views/selfChoose.html";
            //标识mainTopChoose为true表示是从主页面进入的行情
            parentWindow.dealData.set("fromHomePage", true);
        }
    })



}